package com.ll.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ll.auto.JSONAuthentication;
import com.ll.entity.LCommodity;
import com.ll.entity.LType;
import com.ll.entity.LUser;
import com.ll.mapper.LCommodityMapper;
import com.ll.mapper.LTypeMapper;
import com.ll.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import utils.UpPhotoNameUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ll
 * @since 2022-03-15
 */
@RestController
@RequestMapping("/l-commodity")
public class LCommodityController extends JSONAuthentication {
    @Autowired
    LCommodityMapper lCommodityMapper;
    @Autowired
    LTypeMapper lTypeMapper;
    @ApiOperation(value = "添加商品")
    @PostMapping("/add")
    public void addUser(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response, @RequestParam("name") String name, @RequestParam("typeId") int  typeId, @RequestParam("balance") float balance,@RequestParam("value") String value) throws Exception {
        LCommodity lCommodity = new LCommodity();
        lCommodity.setName(name);
        lCommodity.setTypeId(typeId);
        lCommodity.setBalance(balance);
        lCommodity.setValue(value);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("name", name);
        if (lCommodityMapper.selectOne(wrapper) != null) {
            Result error = Result.error().message("该商品名字商品已存在");
            this.WriteJSON(request, response, error);
        } else {
            byte[] bytes = file.getBytes();
            String imageFileName = file.getOriginalFilename();
            String fileName = UpPhotoNameUtils.getPhotoName("img", imageFileName);
            Path path = Paths.get("" + fileName);
            //“C:\\框架\\D4\\d4_pc_ui\\src\\assets\\images\\img\\”为本地目录
            Files.write(path, bytes);//写入文件
            String avatar_url = fileName;
            lCommodity.setPicture(avatar_url);
            response.reset();
            if (lCommodityMapper.insert(lCommodity) > 0) {
                Result ok = Result.ok().message("添加成功");
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error().message("注册失败");
                this.WriteJSON(request, response, error);
            }
        }
    }
    @ApiOperation(value = "删除商品")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = lCommodityMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改商品价格")
    @PostMapping("/updFoodPrice")
    public void updFoodPrice(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int  id, @RequestParam("price") float price) throws Exception {
        LCommodity lCommodity=lCommodityMapper.selectById(id);
        lCommodity.setBalance(price);
        if (lCommodityMapper.updateById(lCommodity) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改商品折扣")
    @PostMapping("/updFoodPriceZheKou")
    public void updFoodPriceZheKou(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int  id, @RequestParam("discount") float discount) throws Exception {
        LCommodity lCommodity=lCommodityMapper.selectById(id);
        lCommodity.setDiscount(discount);
        if (lCommodityMapper.updateById(lCommodity) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找全部商品")
    @PostMapping("/findAllProduct")
    public void findAllProduct(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {

        Page<LCommodity> page = new Page<>(current, size);
        Page<LCommodity> pages = lCommodityMapper.selectPage(page, null);
        List<LCommodity> list = pages.getRecords();
        int total = lCommodityMapper.selectCount(null);
        for(LCommodity a :list){
            a.setLType(lTypeMapper.selectById(a.getTypeId()));
            a.setDisCountPrice(a.getBalance()*a.getDiscount());
        }
        if (list.size() > 0) {
            Result ok = Result.ok().data("product", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有商品，快去添加一个吧");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "按照商品种类查找商品")
    @PostMapping("/findProductByType")
    public void findProductByType(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("TypeName") String TypeName) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("name",TypeName);
        int typeId=lTypeMapper.selectOne(wrapper).getId();
        QueryWrapper wrapper1=new QueryWrapper();
        wrapper1.eq("type_id",typeId);
        Page<LCommodity> page = new Page<>(current, size);
        Page<LCommodity> pages = lCommodityMapper.selectPage(page, wrapper1);
        List<LCommodity> list = pages.getRecords();
        int total = lCommodityMapper.selectCount(wrapper1);
        for(LCommodity a :list){
            a.setLType(lTypeMapper.selectById(a.getTypeId()));
            a.setDisCountPrice(a.getBalance()*a.getDiscount());
        }
        if (list.size() > 0) {
            Result ok = Result.ok().data("product", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有种类，快去添加一个吧");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "根据商品id查找商品")
    @PostMapping("/findProductById")
    public void findProductById(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        LCommodity lCommodity = lCommodityMapper.selectById(id);
        lCommodity.setLType(lTypeMapper.selectById(lCommodity.getTypeId()));
        lCommodity.setDisCountPrice(lCommodity.getBalance()*lCommodity.getDiscount());
        if (lCommodity!=null) {
            Result ok = Result.ok().data("product", lCommodity);
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            error.setMessage("没有商品，快去添加一个吧");
            this.WriteJSON(request, response, error);
        }

    }
}

